﻿namespace SupportSystemUI.Controls.Utility
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using SupportSystemDB;
    using SupportSystemUI.Controls.CustomPages.Crew;
    using SupportSystemUI.Controls.Pages;

    /// <summary>
    /// Interaction logic for MainScreen.xaml
    /// </summary>
    public partial class MainScreen : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainScreen"/> class.
        /// </summary>
        public MainScreen()
        {
            this.InitializeComponent();
            Global.Instance.PagesScreen = this;
        }

        /// <summary>
        /// Pushes a certain page on-top of the stack.
        /// </summary>
        /// <param name="model">Model function to fetch page data from.</param>
        /// <param name="view">View function to fetch page control from.</param>
        public void Push(Func<object> model, Func<object, PageData> view)
        {
            BackgroundWorker dataWorker = new BackgroundWorker(), quoteWorker = new BackgroundWorker();
            this.pagesPanel.Push(new LoadingPage());
            object dataResult = null;

            dataWorker.DoWork += (sender, args) => { args.Result = model(); };
            dataWorker.RunWorkerCompleted += (sender, args) =>
            {
                dataResult = args.Result;
                quoteWorker.RunWorkerAsync();
            };

            quoteWorker.DoWork += (sender, args) =>
            {
                vQuote quote = AppController.Instance.Quotes.GetRandom();
                args.Result = "\"" + quote.Sentence + "\", " + quote.MemberName + ".";
            };
            quoteWorker.RunWorkerCompleted += (sender, args) =>
            {
                if (this.pagesPanel.SlidesCount > 1)
                {
                    this.backIcon.Visibility = Visibility.Visible;
                    this.backLabel.Visibility = Visibility.Visible;
                }

                this.pagesPanel.Push(new BasePage(view(dataResult), args.Result as string));
            };

            dataWorker.RunWorkerAsync();
        }

        /// <summary>
        /// Clears all the slides within this screen.
        /// </summary>
        public void Clear()
        {
            this.pagesPanel.Clear();

            this.backIcon.Visibility = Visibility.Hidden;
            this.backLabel.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// Handler called when the control finishes loading.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">Routed event arguments.</param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.logoutIcon.MouseUp += (sender2, args2) => { Global.Instance.GlobalSlidePanel.Pop(); };
            this.logoutLabel.MouseUp += (sender2, args2) => { Global.Instance.GlobalSlidePanel.Pop(); };

            this.backIcon.MouseUp += (sender2, args2) => this.BackAction();
            this.backLabel.MouseUp += (sender2, args2) => this.BackAction();
            
            this.backIcon.Visibility = Visibility.Hidden;
            this.backLabel.Visibility = Visibility.Hidden;
            
            this.Push(YearItem.Model, YearItem.View);

            this.homeButton.SetCategory(YearItem.Model, YearItem.View);
        }

        /// <summary>
        /// Action executed when the back button is clicked.
        /// </summary>
        private void BackAction()
        {
            if (this.pagesPanel.SlidesCount > 4)
            {
                this.pagesPanel.Pop();
                this.pagesPanel.Pop();
            }
            else if (this.pagesPanel.SlidesCount == 4)
            {
                this.pagesPanel.Pop();
                this.pagesPanel.Pop();
                this.backIcon.Visibility = Visibility.Hidden;
                this.backLabel.Visibility = Visibility.Hidden;
            }
        }
    }
}